<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        *{margin:0;padding:0}
        img{
            width:240px
        }
        .item{
            border:1px solid #eee;
            padding:20px;
            float:left
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="item">
            <img src="images/01.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/02.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/03.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/04.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/05.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/06.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/07.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/08.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/09.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/10.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/11.jpg" alt="">
        </div>
        <div class="item">
            <img src="images/12.jpg" alt="">
        </div>
    </div>
    <script>
        /* 1.一排能放置几张图片 */
        window.onload=function(){
            var ww=$(window).width();
            var itemWidth=$(".item").outerWidth()  
            console.log(itemWidth)  
            var num=Math.floor(ww/itemWidth)   
            console.log(num)
            /* 2.将第一排的高度添加到一个数组中 */
            var heightArr=[];
            $(".item").each((index,item)=>{
                if(index<num){
                    var item=$(item).outerHeight()
                    heightArr.push(item)
                }else{
                    /* 3.从数组高度最小的地方添加图片 */
                    var minHeight=Math.min(...heightArr);  //最小高度
                    console.log(minHeight)
                    var minIndex=heightArr.indexOf(minHeight);
                    var offsetLeft=$(".item").eq(minIndex).offset().left;  //最小高度的item距离左边的偏移量
                    $(".item").eq(index).css({position:'absolute',top:minHeight,left:offsetLeft})
                }
                /* 4.重新设置数组的最小高度 */
                var height=$(".item").eq(index).outerHeight();
                heightArr[minIndex]=minHeight+height;
            })
        }

    </script>
</body>
</html>